package jsyf.oa.web.device;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportExcel;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.device.DeviceServiceFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by y on 2018/4/16.
 */
@Api(value = "服务设备接口", description = "服务设备接口")
@Controller
@RequestMapping("/deviceService")
public class DeviceServiceController {
    @Resource
    private DeviceServiceFacade deviceServiceFacade;
    @ResponseBody
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "添加设备",
            notes = "添加设备 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "jsonData:''", dataType = "String", paramType = "body")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        String records = this.deviceServiceFacade.add(jsonData);
        JSONObject obj = new JSONObject();
            obj.put("records", records);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }
    @ResponseBody
    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "通过id物理删除",
            notes = "通过id物理删除 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void removeById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        Boolean records = this.deviceServiceFacade.removeById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records.toString()));
    }
    @ResponseBody
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改信息",
            notes = "修改信息 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "jsonData:''", dataType = "String", paramType = "body")
    public void update(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        Boolean records = this.deviceServiceFacade.update(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records.toString()));
    }
    @ResponseBody
    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "通过id查询",
            notes = "通过id查询 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void getById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject JSONObj = this.deviceServiceFacade.getById(jsonData);
        if (null != JSONObj) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(JSONObj));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("查找失败"));
        }
    }
    @ResponseBody
    @RequestMapping(value = "/getDeviceList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询列表",
            notes = "查询列表 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'companyName':'','brandName':'','deviceTypeName':''}", dataType = "String", paramType = "body")
    public void getDeviceList(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject records = this.deviceServiceFacade.getDeviceList(jsonData);
        if (null != records) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("查找失败"));
        }
    }
    @ResponseBody
    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "通过id逻辑删除设备",
            notes = "通过id逻辑删除设备 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void delById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = new JSONObject();
        obj.put("isOk", this.deviceServiceFacade.delById(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }
    @RequestMapping(value = "/getDownByCondition.json")
    public void getDownByCondition(HttpServletResponse response,@RequestParam String companyName,@RequestParam String deviceTypeName,@RequestParam String brandName,@RequestParam String modelName,@RequestParam String key) throws Exception {
       List<JSONObject> records = deviceServiceFacade.getByCondition(companyName,deviceTypeName,brandName,modelName,key);
        getDownExcel(records, response);
    }
    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response) throws Exception {
        //导出表名、列名
        String title = "服务设备列表";
        String[] rowsName = new String[]{"公司名称", "设备类型", "品牌", "型号", "序列号", "自销/转入",
                "出厂年份", "交机日期","小时数", "客户姓名", "客户电话", "备注", "设备管理员", "设备管理员电话", "设备操作手", "设备操作手电话"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.get("companyName");
            objs[1] = jsonObject.get("deviceTypeName");
            objs[2] = jsonObject.get("brandName");
            objs[3] = jsonObject.get("modelName");
            objs[4] = jsonObject.get("sequence");
            objs[5] = jsonObject.get("saledType");
            objs[6] = jsonObject.get("years");
            objs[7] = jsonObject.get("deliveryDate");
            objs[8] = jsonObject.get("hours");
            objs[9] = jsonObject.get("customerName");
            objs[10] = jsonObject.get("customerTel");
            objs[11] = jsonObject.get("remark");
            objs[12] = jsonObject.get("adminName");
            objs[13] = jsonObject.get("adminTel");
            objs[14] = jsonObject.get("operatorName");
            objs[15] = jsonObject.get("operatorTel");
            dataList.add(objs);
        }
        ExportExcel ex = new ExportExcel(title, rowsName, dataList, response);
        ex.export();
    }
}
